
class element:

    def __init__(self,name):

        self.name = name
        self.num_childs = 0
        self.parent = None
        self.level = 1
        self.local_order = 1
        self.path = "./" + name
        self.num_leafs = 0
        self.sib_order = None
        self.childs_sib_order = None
        self.max_sib = None
        self.min_sib = None
    
    def set_parent(self,parent):

        self.parent = parent
        if (self.parent != None):
            self.level = self.parent.get_level() + 1
            self.local_order = self.parent.add_child()
            self.path = self.parent.get_path() + self.path
#        self.parent.add_child()

    def add_child(self):
        
        self.num_childs = self.num_childs + 1
        return self.num_childs

    def is_leaf_node(self):
        
        if self.num_childs != 0:
            return 0

        if self.parent != None:
            self.parent.add_leaf_node()

        return 1

    def add_leaf_node(self):

        self.num_leafs = self.num_leafs + 1

    def get_local_order(self):

        return self.local_order

    def get_parent(self):

        return self.parent

    def get_path(self):

        return self.path

    def get_level(self):
        
        return self.level

    def have_leafs(self):
        
        if self.childs_sib_order == None:
            return 0
        else:
            return 1

    def end_leaf(self,document):

        if self.parent == None:
            self.sib_order = 1
            return

        if self.parent.have_leafs() == 0:

            self.sib_order = document.add_sibling()
            self.parent.set_childs_sib_order(self.sib_order)
            
        else:

            self.sib_order = self.parent.get_childs_sib_order()

        if self.parent.get_max_sib_order() == None:
            self.parent.set_max_sib(self.sib_order)
            self.parent.set_min_sib(self.sib_order)
        else:
            if self.sib_order > self.parent.get_max_sib_order():
                self.parent.set_max_sib(self.sib_order)

    def end_ancestor(self):

        if (self.parent != None):
            if self.parent.get_max_sib_order() == None:
                self.parent.set_max_sib(self.max_sib)
                self.parent.set_min_sib(self.min_sib)
            else:
                if self.max_sib > self.parent.get_max_sib_order():
                    self.parent.set_max_sib(self.max_sib)

    def set_max_sib(self,value):
        
        self.max_sib = value

    def set_min_sib(self,value):

        self.min_sib = value

    def get_sib_order(self):
        
        return self.sib_order
            
    def get_childs_sib_order(self):
        
        return self.childs_sib_order
        
    def set_childs_sib_order(self,value):
        
        self.childs_sib_order = value

    def get_max_sib_order(self):

        return self.max_sib
    
    def get_min_sib_order(self):
        
        return self.min_sib

    def get_name(self):

        return self.name
